//
//  PywAccountManagerViewController.m
//  PywSdk
//
//  Created by smallwave on 16/8/26.
//  Copyright © 2016年 zero. All rights reserved.
//

#import "PywAccountManagerViewController.h"
#import "PywAccountListCollectionViewCell.h"
#import "PywMasonry.h"
#import "PywAccountListModel.h"
#import "PywPlatformMain.h"
#import "PywAppInfo.h"
#import "MutableDictionary.h"

#import "PywGetAccountListApi.h"
#import "PywLoginAccountApi.h"
#import "PywAddAccountApi.h"
#import "PywDeleteAccountApi.h"
#import "PywGetSmsCodeApi.h"

#import "PywBundGameAccountController.h"
#import "PywAbsolutedLoginPwdController.h"
#import "PywUnbundAccountCheckPhoneController.h"

#import "PywLogoutManger.h"
#import "PywCPNotifications.h"
#import "PywSdk+login.h"
#import "UIImage+Pyw.h"
#import "ApiClientConst.h"
#import "PywFileManager.h"
#import "PywUploadAccountLoginManager.h"

@interface PywAccountManagerViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout,UIActionSheetDelegate,UIPopoverPresentationControllerDelegate>

@property (nonatomic, strong) UICollectionView                  *collectionView;
@property (nonatomic, copy) NSMutableArray                      *accountArray;      // 保存游戏账号数组

@property (nonatomic, strong) PywGetAccountListApi              *getAccountListApi; // 游戏账号列表
@property (nonatomic, strong) PywAddAccountApi                  *addAccountApi;     // 游戏账号新建
@property (nonatomic, strong) PywDeleteAccountApi               *deleteAccountApi;  // 删除游戏账号
@property (nonatomic, strong) PywGetSmsCodeApi                  *getSmsCodeApi;     // 获取验证码

@property (nonatomic,strong) PywAccountListModel *currentListModel;//当前选中的Account
@property (nonatomic, strong) UIActionSheet *actionSheet;
@end

@implementation PywAccountManagerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUp];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self requestAccountList];
}
- (void)setUp{
    [self setLeftBarButtonItem];
    self.navigationItem.rightBarButtonItem = nil;
    self.title = @"账号管理";
    self.automaticallyAdjustsScrollViewInsets = YES;
    self.view.backgroundColor = RGB(232, 232, 232, 1);
    
    [self.view addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.top.bottom.right.left.offset(0);
    }];
}
- (void)setLeftBarButtonItem{
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn setImage:[[UIImage imageWithName:@"icon_return_normal"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [backBtn setImage:[[UIImage imageWithName:@"icon_return_normal"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateHighlighted];
    [backBtn sizeToFit];
    // 设置内容属性
    backBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    backBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
    [backBtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
}
/*
 * 重现导航栏，点击“返回”按钮返回用户中心，发送“用户中心”请求刷新通知
 */
- (void)backAction:(UIButton *)sender{
    [self popView];
    [[NSNotificationCenter defaultCenter] postNotificationName:ReloadUserCenterData object:nil];
}
#pragma mark - UICollectionViewDataSource
static NSString * CellIdentifier = @"UICollectionViewCell";
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    PywAccountListCollectionViewCell * cell = (PywAccountListCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    if (indexPath.item >= self.accountArray.count){
        
        [cell reloadCellData:@" + 添加游戏账号"];
    }else{
        
        PywAccountListModel *account = self.accountArray[indexPath.row];
        NSString *subTitle = nil;
        NSString *accessibilityId = nil;
        PLog(@"%d", [account.account isEqualToString:[PywPlatformMain sharedPlatformMain].account]);
        if ([account.is_new integerValue] == 1) {
            subTitle = @"新";
            accessibilityId = @"manage_new_account";
        }
        if ([account.is_freeze integerValue] == 1) {
            subTitle = @"账号被冻结";
            accessibilityId = @"manage_frozen_account";
        }
        if ([account.account isEqualToString:[PywPlatformMain sharedPlatformMain].account]){
            subTitle = @"当前登录";
            accessibilityId = @"manage_current_account";
        }else{
            accessibilityId = @"manage_account";
        }
        [cell  cellWithData:account.account andSubTitle:subTitle accessibilityId:accessibilityId];
    }
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.accountArray.count + 1;
}

#pragma mark - UICollectionDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (!iOS8) return;
    if (indexPath.item >= self.accountArray.count) {
        self.currentListModel = nil;
        [self addAccountActionSheet:collectionView IndexPath:indexPath];//新建账号
    }else{
        PywAccountListModel *listModel = [self.accountArray objectAtIndex:indexPath.row];
        self.currentListModel = listModel;
        if ([self.currentListModel.account isEqualToString:[PywPlatformMain sharedPlatformMain].account]){//选择当前账号
            
            [self currentAccountActionSheet:collectionView IndexPath:indexPath];
            
        }else {//选择其他账号
            [self otherAccountActionSheet:self.currentListModel CollectionView:collectionView IndexPath:indexPath];
        }
    }
}

/**
 *  点击添加游戏账号
 */
- (void)addAccountActionSheet:(UICollectionView *)collectionView IndexPath:(NSIndexPath *)indexPath
{
    UIAlertController *alertView = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    WEAK_SELF;
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"绑定账号" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        PywBundGameAccountController *gameAccount = [[PywBundGameAccountController alloc] initWithFinishBlock:^{
            
            [weakSelf requestAccountList];
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }];
        [weakSelf.navigationController pushViewController:gameAccount animated:YES];
    }];
    [action1 setAccessibilityLabel:@"manage_binaccount"];
    
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"新建账号" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [weakSelf requestNewAccount];
    }];
    [action2 setAccessibilityLabel:@"manage_newaccount"];
    
    UIAlertAction *actioncancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        [weakSelf.collectionView reloadData];
    }];
    [actioncancel setAccessibilityLabel:@"manage_out"];
    
    [alertView addAction:action1];
    [alertView addAction:action2];
    [alertView addAction:actioncancel];
    
    // 在iPad中没有添加上面方法，运行时会出现崩溃
    UIPopoverPresentationController *popover = alertView.popoverPresentationController;
    if (popover)
    {
        UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
        popover.sourceView = cell; //下拉式对话框 弹出的位置
        popover.sourceRect = cell.bounds;
        popover.permittedArrowDirections = UIPopoverArrowDirectionUp;
    }
    
    [self presentViewController:alertView animated:YES completion:NULL];
    
//    UIActionSheet *actionsheet = [[UIActionSheet alloc] initWithTitle:nil
//                                                             delegate:self
//                                                    cancelButtonTitle:@"取消"
//                                               destructiveButtonTitle:nil otherButtonTitles:nil];
//    actionsheet.tag = 10034;
//    [actionsheet addButtonWithTitle:@"绑定账号"];
//    [actionsheet addButtonWithTitle:@"新建账号"];
//    [actionsheet showInView:self.view];
    
}
/**
 *  点击当前账号
 */
-(void)currentAccountActionSheet:(UICollectionView *)collectionView IndexPath:(NSIndexPath *)indexPath
{
    UIAlertController *alertView = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    WEAK_SELF;
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"独立登录密码" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        PywAbsolutedLoginPwdController *resetAbsolutePwdVC = [[PywAbsolutedLoginPwdController alloc] initWithGameAccount:self.currentListModel.account];
        [weakSelf.navigationController pushViewController:resetAbsolutePwdVC animated:YES];
    }];
    [action1 setAccessibilityLabel:@"manage_alone_pwsd"];
    
    
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"解绑此账号" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        // 点击Item，先发送“获取验证码”请求成功后，跳转控制器
        [weakSelf requestGetSmsCodeSuccessWithAccount:self.currentListModel.account];
    }];
    [action2 setAccessibilityLabel:@"manage_bundaccount"];
    
    UIAlertAction *actioncancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        [weakSelf.collectionView reloadData];
    }];
    [actioncancel setAccessibilityLabel:@"manage_out"];
    
    [alertView addAction:action1];
    [alertView addAction:action2];
    [alertView addAction:actioncancel];
    
    // 在iPad中没有添加上面方法，运行时会出现崩溃
    UIPopoverPresentationController *popover = alertView.popoverPresentationController;
    if (popover)
    {
        UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
        popover.sourceView = cell; //下拉式对话框 弹出的位置
        popover.sourceRect = cell.bounds;
        popover.permittedArrowDirections = UIPopoverArrowDirectionUp;
    }
    
    [self presentViewController:alertView animated:YES completion:NULL];
    
//    UIActionSheet *actionsheet = [[UIActionSheet alloc] initWithTitle:nil
//                                                             delegate:self
//                                                    cancelButtonTitle:@"取消"
//                                               destructiveButtonTitle:nil otherButtonTitles:nil];
//    actionsheet.tag = 10035;
//    [actionsheet addButtonWithTitle:@"独立登录密码"];
//    [actionsheet addButtonWithTitle:@"解绑此账号"];
//    [actionsheet showInView:self.view];
    
}

/**
 *  点击其他账号
 */
-(void)otherAccountActionSheet:(PywAccountListModel *)account CollectionView:(UICollectionView *)collectionView IndexPath:(NSIndexPath *)indexPath
{
    UIAlertController *alertView = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    WEAK_SELF;
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"登录此账号" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [weakSelf requestLoginAccount:weakSelf.currentListModel.account];
    }];
    [action1 setAccessibilityLabel:@"manage_loginaccount"];
    
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"独立登录密码" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        PywAbsolutedLoginPwdController *resetAbsolutePwdVC = [[PywAbsolutedLoginPwdController alloc] initWithGameAccount:weakSelf.currentListModel.account];
        [weakSelf.navigationController pushViewController:resetAbsolutePwdVC animated:YES];
    }];
    [action2 setAccessibilityLabel:@"manage_alone_pwsd"];
    
    UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"解绑此账号" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        // 点击Item，先发送“获取验证码”请求成功后，跳转控制器
        [weakSelf requestGetSmsCodeSuccessWithAccount:self.currentListModel.account];
    }];
    [action3 setAccessibilityLabel:@"manage_bundaccount"];
    
    UIAlertAction *action4 = [UIAlertAction actionWithTitle:@"删除账号(谨慎操作)" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [weakSelf requestDeleteAccountWithAccount:weakSelf.currentListModel.account];
    }];
    [action4 setAccessibilityLabel:@"manage_deleteaccount"];
    
    UIAlertAction *actioncancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        [weakSelf.collectionView reloadData];
    }];
    [actioncancel setAccessibilityLabel:@"manage_out"];
    
    [alertView addAction:action1];
    [alertView addAction:action2];
    [alertView addAction:action3];
    // 当允许显示，并且点击的是非当前登录账号时，才显示“删除账号”
    if ([PywPlatformMain sharedPlatformMain].is_deleteAccountBtn
        && ![account.account isEqualToString:[PywPlatformMain sharedPlatformMain].account]) {
        [alertView addAction:action4];
    }
    [alertView addAction:actioncancel];
    
    // 在iPad中没有添加上面方法，运行时会出现崩溃
    UIPopoverPresentationController *popover = alertView.popoverPresentationController;
    if (popover)
    {
        UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
        popover.sourceView = cell; //下拉式对话框 弹出的位置
        popover.sourceRect = cell.bounds;
        popover.permittedArrowDirections = UIPopoverArrowDirectionUp;
    }
    
    [self presentViewController:alertView animated:YES completion:NULL];
    
//    UIActionSheet *actionsheet = [[UIActionSheet alloc] initWithTitle:nil
//                                                             delegate:self
//                                                    cancelButtonTitle:@"取消"
//                                               destructiveButtonTitle:nil otherButtonTitles:nil];
//    actionsheet.tag = 10036;
//    [actionsheet addButtonWithTitle:@"登录此账号"];
//    [actionsheet addButtonWithTitle:@"独立登录密码"];
//    [actionsheet addButtonWithTitle:@"解绑此账号"];
//    // 当允许显示，并且点击的是非当前登录账号时，才显示“删除账号”
//    if ([PywPlatformMain sharedPlatformMain].is_deleteAccountBtn
//        && ![account.account isEqualToString:[PywPlatformMain sharedPlatformMain].account]) {
//        [actionsheet addButtonWithTitle:@"删除账号(谨慎操作)"];
//    }
//
//    [actionsheet showInView:self.view];

}
- (void)prepareForPopoverPresentation:(UIPopoverPresentationController *)popoverPresentationController
{
    NSLog(@"popoverPresentationController = %@",popoverPresentationController);
}

#pragma mark - actionSheetTintColor
// 竖屏布局
- (void)verticalScreenControlsDistribution {
    [self actionSheetTintColor:self.actionSheet];
}
// 横屏布局
- (void)horizontalScreenControlsDistribution {
    [self actionSheetTintColor:self.actionSheet];
}
/**
 *  设置 actionSheetTintColor
 *
 *  @param actionSheet <#actionSheet description#>
 */
- (void)actionSheetTintColor:(UIActionSheet *)actionSheet{
    SEL selector = NSSelectorFromString(@"_alertController");
    if ([actionSheet respondsToSelector:selector])//ios8
    {
        UIAlertController *alertController = [actionSheet valueForKey:@"_alertController"];
        if ([alertController isKindOfClass:[UIAlertController class]])
        {
            alertController.view.tintColor = [UIColor grayColor];
        }
    } else//ios7
    {
        for( UIView * subView in actionSheet.subviews )
        {
            
            if( [subView isKindOfClass:[UIButton class]] )
            {
                UIButton * btn = (UIButton*)subView;
                [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            }
        }
    }
}

///  使用 UIAlertController 替换了 UIActionSheet 的实现，故以下代理方式暂时无效
#pragma mark - UIActionSheetDelegate
-(void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
    self.actionSheet = actionSheet;
    [self actionSheetTintColor:actionSheet];
}
- (void)didPresentActionSheet:(UIActionSheet *)actionSheet
{
    [self actionSheetTintColor:actionSheet];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSInteger tag = actionSheet.tag;
    switch (tag) {
        case 10034://点击新建账号
        {
            switch (buttonIndex) {
                case 0://取消选中边框
                {
//                    [actionSheet setAccessibilityIdentifier:@"manage_out"];
                    [self.collectionView reloadData];
                }
                    break;
                case 1://绑定账号
                {
//                    [actionSheet setAccessibilityIdentifier:@"manage_binaccount"];
                    WEAK_SELF;
                    PywBundGameAccountController *gameAccount = [[PywBundGameAccountController alloc] initWithFinishBlock:^{
                        
                        [weakSelf requestAccountList];
                        [weakSelf.navigationController popViewControllerAnimated:YES];
                    }];
                    [self.navigationController pushViewController:gameAccount animated:YES];
                }
                    break;
                case 2://新建账号
                {
//                    [actionSheet setAccessibilityIdentifier:@"manage_newaccount"];
                    [self requestNewAccount];
                }
                    break;
                    
                default:
                    break;
            }
        }
            break;
        case 10035://点击当前账号
        {
            switch (buttonIndex) {
                case 0://取消选中边框
                {
//                    [actionSheet setAccessibilityIdentifier:@"manage_out"];
                    [self.collectionView reloadData];
                }
                    break;
                case 1://独立登录密码
                {
//                    [actionSheet setAccessibilityIdentifier:@"manage_alone_pwsd"];
                    PywAbsolutedLoginPwdController *resetAbsolutePwdVC = [[PywAbsolutedLoginPwdController alloc] initWithGameAccount:self.currentListModel.account];
                    [self.navigationController pushViewController:resetAbsolutePwdVC animated:YES];
                }
                    break;
                case 2://解绑此账号
                {
//                    [actionSheet setAccessibilityIdentifier:@"manage_bundaccount"];
                    // 点击Item，先发送“获取验证码”请求成功后，跳转控制器
                    [self requestGetSmsCodeSuccessWithAccount:self.currentListModel.account];
                }
                    break;
                    
                default:
                    break;
            }
        }
            break;
        case 10036://点击其他账号
        {
            switch (buttonIndex) {
                case 0://取消选中边框
                {
//                    [actionSheet setAccessibilityIdentifier:@"manage_out"];
                    [self.collectionView reloadData];
                }
                    break;
                case 1://登录此账号
                {
//                    [actionSheet setAccessibilityIdentifier:@"manage_loginaccount"];
                    [self requestLoginAccount:self.currentListModel.account];
                }
                    break;
                case 2://独立登录密码
                {
//                    [actionSheet setAccessibilityIdentifier:@"manage_alone_pwsd"];
                    PywAbsolutedLoginPwdController *resetAbsolutePwdVC = [[PywAbsolutedLoginPwdController alloc] initWithGameAccount:self.currentListModel.account];
                    [self.navigationController pushViewController:resetAbsolutePwdVC animated:YES];
                }
                    break;
                case 3://解绑此账号
                {
//                    [actionSheet setAccessibilityIdentifier:@"manage_bundaccount"];
                    // 点击Item，先发送“获取验证码”请求成功后，跳转控制器
                    [self requestGetSmsCodeSuccessWithAccount:self.currentListModel.account];
                }
                    break;
                case 4://删除此账号
                {
//                    [actionSheet setAccessibilityIdentifier:@"manage_deleteaccount"];
                    [self requestDeleteAccountWithAccount:self.currentListModel.account];
                }
                    break;
                    
                default:
                    break;
            }
        }
            break;
            
        default:
            break;
    }
}

#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(140, 40);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(20, 15, 20, 15);
}
#pragma mark - RequestApi
/**
 *  获取验证码
 */
- (void)requestGetSmsCodeSuccessWithAccount:(NSString *)account{
    NSString *phoneNumber = [[PywPlatformMain sharedPlatformMain].passport stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    NSString *smsType = [[NSString stringWithFormat:@"%d",SMSCODETYPE_UNBUNDLE_ACCOUNT] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    
    MutableDictionary *params = [[MutableDictionary alloc] init];
    [params setObject:phoneNumber forKey:@"phone"];
    [params setObject:smsType forKey:@"smstype"];
    
    [self.getSmsCodeApi requestGetSmsCodeWithParams:params success:^(NSString *status, id anyObjct) {
        
        PywUnbundAccountCheckPhoneController *unBundGameAccountVC = [[PywUnbundAccountCheckPhoneController alloc] initWithGameAccount:account
                                                                                                                              smsType:smsType];
        [self.navigationController pushViewController:unBundGameAccountVC animated:YES];
    } fail:^(NSError *error) {
        
    }];
}
/**
 *  请求删除游戏账号
 *
 *  @param account 选择要删除的游戏账号
 */
- (void)requestDeleteAccountWithAccount:(NSString *)account{
    [self.deleteAccountApi requestDeleteAccountWithAccount:account Success:^(NSString *status, id anyObjct) {
        
        [self requestAccountList];
    } fail:^(NSError *error) {
        
    }];
}
/**
 *  请求游戏账号列表
 */
- (void)requestAccountList{
    [self.getAccountListApi requestGetAccountListWithSuccess:^(NSString *status, id anyObjct) {
        
        self.accountArray = [PywAccountListModel arrayOfModelsFromDictionaries:anyObjct error:nil];
        [self.collectionView reloadData];
    } fail:^(NSError *error) {
        
    }];
}
/**
 *  切换账号登录
 */
- (void)requestLoginAccount:(NSString *)account
{
    
    [self dismissViewControllerAnimated:YES completion:NULL];
    // 发送注销完成的通知
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
    [dict setValue:@"注销成功" forKey:@"info"];
    [dict setValue:[NSString stringWithFormat:@"%d",PYW_LOGOUT_STATE_NORMAL] forKey:@"result"];
    [[NSNotificationCenter defaultCenter] postNotificationName:kPywCPLogoutNotification object:nil userInfo:dict];
    
    // 注销账号，保存当前登录账号结束时间
    [[PywUploadAccountLoginManager manager] saveAccountLoginEndTime];
    
    [[PywLogoutManger manger] clearLogin];
    
    [PywPlatformMain sharedPlatformMain].changeAccount = account;
    
    [[PywFileManager manager] setFileValue:[PywPlatformMain sharedPlatformMain].changeAccount
                                forFileKey:@"changeAccount"];
    
}
/**
 *  新建游戏账号
 */
- (void)requestNewAccount{
    [self.addAccountApi requestAddAccountWithSuccess:^(NSString *status, id anyObjct) {
        
        [self requestAccountList];
    } fail:^(NSError *error) {
       
    }];
}

#pragma mark - Setter & Getter
- (NSMutableArray *)accountArray{
    if (!_accountArray) {
        _accountArray = [NSMutableArray array];
    }
    return _accountArray;
}
- (PywGetAccountListApi *)getAccountListApi{
    if (!_getAccountListApi) {
        _getAccountListApi = [PywGetAccountListApi manager];
    }
    return _getAccountListApi;
}
- (PywAddAccountApi *)addAccountApi{
    if (!_addAccountApi) {
        _addAccountApi = [PywAddAccountApi manager];
    }
    return _addAccountApi;
}
- (PywDeleteAccountApi *)deleteAccountApi{
    if (!_deleteAccountApi) {
        _deleteAccountApi = [PywDeleteAccountApi manager];
    }
    return _deleteAccountApi;
}
- (PywGetSmsCodeApi *)getSmsCodeApi{
    if (!_getSmsCodeApi) {
        _getSmsCodeApi = [PywGetSmsCodeApi manager];
    }
    return _getSmsCodeApi;
}

#pragma mark - Lazy
- (UICollectionView *)collectionView{
    if (!_collectionView ) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        _collectionView = collectionView ;
        collectionView.delegate = self;
        collectionView.dataSource = self;
        collectionView.backgroundColor = RGB(232, 232, 232, 1);
        [collectionView registerClass:[PywAccountListCollectionViewCell class] forCellWithReuseIdentifier:@"UICollectionViewCell"];
    }
    return _collectionView;
}

@end
